import { computed, ref } from "vue";
import { defineStore } from "pinia";
import defaultAvatar from "@/assets/images/default_user.png"; 

export const useUserStore = defineStore(
  "user",
  () => {
    const token = ref("");
    const username = ref("");
    const uid = ref("");
    const email = ref("");
    const phone = ref("");
    const sex = ref("");
    const birthday = ref("");
    const avatarUrl = ref("");
    const description = ref("");
    const status = ref("");
    const isSinger = ref(0);
    const userRole = ref("");
    const defaultAvatarUrl = defaultAvatar;

    const isLogin = computed(() => {
      if (token.value === "") {
        return false;
      }
      return true;
    });

    const setToken = (value: string) => {
      token.value = value;
    };
    const setUsername = (value: string) => {
      username.value = value;
    };
    const setUid = (value: string) => {
      uid.value = value;
    };
    const setUserInfo = (user: any) => {
      uid.value = user.uid;
      username.value = user.uname;
      email.value = user.email;
      phone.value = user.phone;
      sex.value = user.sex;
      birthday.value = user.birthday;
      avatarUrl.value = user.avatarUrl;
      description.value = user.description;
      status.value = user.status;
      isSinger.value = user.isSinger;
      userRole.value = user.userRole;
    };
    const logout = () => {
      token.value = "";
      username.value = "";
      uid.value = "";
      email.value = "";
      phone.value = "";
      sex.value = "";
      birthday.value = "";
      avatarUrl.value = "";
      description.value = "";
      status.value = "";
      isSinger.value = 0;
      userRole.value = "";
    };
    const setAvatar = (value: string) => {
      avatarUrl.value = value;
    };
    return {
      token,
      setToken,
      isLogin,
      username,
      setUsername,
      uid,
      setUid,
      email,
      phone,
      sex,
      birthday,
      avatarUrl,
      description,
      status,
      isSinger,
      userRole,
      setUserInfo,
      logout,
      setAvatar,
      defaultAvatarUrl,
    };
  },
  {
    persist: true,
  }
);
